//Centering robot: Centers in the middle of a circular arena //Authors: Ravi Prakash, Alexis Mackenzie and Zubia Jamil //Motor left on Port 1 //Motor right on Port 3 #define MEMSIZE 40 #define MOTORLEFT 1 #define MOTORRIGHT 3 #define THRESHOLD 200.0 void main() { int i, flag; float sum1, sum2, memory[MEMSIZE],err; sonar_init(); for(i=0; i=sum2 || (sum2-sum1)<=THRESHOLD) { //printf("%f %d %d\n",sum1-sum2,(int)((sum2-sum1)*0.01),flag); printf("turning %d\n",flag); motor(MOTORLEFT, 0); motor(MOTORRIGHT, 100); flag++; // continue; } if(sum1THRESHOLD) { //printf("%f %d %d\n",sum1-sum2,(int)((sum2-sum1)*0.01),flag); printf("moving straight\n"); err=sum2-sum1-THRESHOLD; motor(MOTORLEFT, 20+(int)(.08*err)); motor(MOTORRIGHT, 100); // msleep((long)((sum2-sum1)*0.01)); if(flag>0) flag--; } //if(flag>150) break; } motor(MOTORLEFT, 0); motor(MOTORRIGHT,0); beep(); beep(); } /* Handyboard Sonar Driver Originally By Acroname Modified by Rand Voorhies - 10/07 - to use timer/counter 2 on PA1 so that TC3 on PA0 may be reserved for the compass. For an overview of the SRF04, please visit: http://www.robot-electronics.co.uk/htm/srf04tech.htm http://www.acroname.com/examples/10034/10034.html Pin Connections: Echo Pulse Output: Digital 8 Trigger Pulse Input: Digital 9 */ void sonar_init() { bit_set(0x1026, 0x80); /* set Digital Input #9 as output */ bit_clear(0x1021, 7); /* at TCTL2, */ bit_set(0x1021, 8); /* set TIC2 for falling edge */ } int sonar_sample() { int start_time; int dist; poke(0x1023, 2); /* clear tic2 flag */ start_time = peekword(0x100e); /* capture start time */ bit_set(0x1000, 0x80); /* send init pulse */ bit_clear(0x1000, 0x80); /* wait until receive echo */ while (peek(0x1000) & 0x02) { if ((peekword(0x100e) - start_time) < 0) { /* if too much time has elapsed, abort */ return -1; } defer(); /* let others run while waiting */ } msleep(10L); /* give unit time to reset */ dist= peekword(0x1012)-start_time; return dist; /* tic3 has time of echo */ } /* By Andrew Chanler - Fall 2003 Handyboard driver for using Devantech Magnetic Compass - CMPS03 refer to http://www.cs.uml.edu/~achanler/robotics/ Pin Connections: PWM: Digital 7 */ #use "alc_cmps03.icb" int compass() { int x; float z; int y; float degree; x = compass_driver(0); z = ((float) ( 0x0F & x) )+( 256.0*((float) ( x >> 8) )) ; if((z < 7440.0)&&(compass_wrap)) z+=65536.0; degree = ((z - 1792.0)*360.0)/71183.0; y = (int) (degree+.5); y = y % 360; return y; }